package io.renren.modules.generator.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.CourseEntity;
import io.renren.modules.generator.entity.LdcodeEntity;
import io.renren.modules.generator.service.CourseService;
import io.renren.modules.generator.service.LdcodeService;
import io.renren.util.JsonUtil;

@RestController
public class BaseController{
	
	@Autowired
	private LdcodeService ldcodeService;
	@Autowired
	private CourseService courseService;

	//下拉选择
	@RequestMapping("generator/codeSelect")
	@ResponseBody
	public String codeSelect(HttpServletRequest request){
		
		String codetype = request.getParameter("codetype");
		Map<String,Object> map = new HashMap<String,Object>();
		
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		
		if (codetype.contains("class")) {

			List<CourseEntity> tResultList = courseService.queryList(map);

			for (CourseEntity course : tResultList) {
				Map<String,String> returnMap = new HashMap<String,String>();
				returnMap.put("code", course.getClassno());
				returnMap.put("codename", course.getName());
				list.add(returnMap);
			}
		} else {
			map.put("codetype", codetype);			
			List<LdcodeEntity> tResultList = ldcodeService.queryList(map);
			for (LdcodeEntity ldcode : tResultList) {
				Map<String,String> returnMap = new HashMap<String,String>();
				returnMap.put("code", ldcode.getCode());
				returnMap.put("codename", ldcode.getCodename());
				list.add(returnMap);
			}
		}
		if(list!=null && list.size()>0){
			return JsonUtil.objectToJson(list);
		}else{
			return "";
		}

	}

}
